
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.document.operation.impl {
import org.waveprotocol.wave.model.document.operation.AnnotationBoundaryMap;
import org.waveprotocol.wave.model.document.operation.Attributes;
import org.waveprotocol.wave.model.document.operation.AttributesUpdate;
import org.waveprotocol.wave.model.document.operation.DocumentOperationCursor;
import org.waveprotocol.wave.model.document.operation.automaton.DocOpAutomaton;
import org.waveprotocol.wave.model.document.operation.automaton.ValidationResult;
import org.waveprotocol.wave.model.document.operation.automaton.violation.ViolationCollector;

public class IllFormedDocOpCursor implements DocumentOperationCursor {
	
	private var accu:Array;
	private var a:DocOpAutomaton;
	private var v:ViolationCollector;
	
	
	public function IllFormedDocOpCursor(accu:Array, a:DocOpAutomaton, v:ViolationCollector) {
		this.accu = accu;
		this.a = a;
		this.v = v;
	}
	
	
	function abortIfIllFormed():void {
		if (accu[0].isIllFormed()) {
			throw DocOpValidator.ILL_FORMED;
		}
	}
	
	
	public function characters(s:String):void {
		accu[0] = accu[0].mergeWith(a.checkCharacters(s, v));
		abortIfIllFormed();
		a.doCharacters(s);
	}
	
	
	public function deleteCharacters(chars:String):void {
		accu[0] = accu[0].mergeWith(a.checkDeleteCharacters(chars, v));
		abortIfIllFormed();
		a.doDeleteCharacters(chars);
	}
	
	
	public function deleteElementEnd():void {
		accu[0] = accu[0].mergeWith(a.checkDeleteElementEnd(v));
		abortIfIllFormed();
		a.doDeleteElementEnd();
	}
	
	
	public function deleteElementStart(type:String, attrs:Attributes):void {
		accu[0] = accu[0].mergeWith(a.checkDeleteElementStart(type, attrs, v));
		abortIfIllFormed();
		a.doDeleteElementStart(type, attrs);
	}
	
	
	public function replaceAttributes(oldAttrs:Attributes, newAttrs:Attributes):void {
		accu[0] = accu[0].mergeWith(a.checkReplaceAttributes(oldAttrs, newAttrs, v));
		abortIfIllFormed();
		a.doReplaceAttributes(oldAttrs, newAttrs);
	}
	
	
	public function retain(itemCount:int):void {
		accu[0] = accu[0].mergeWith(a.checkRetain(itemCount, v));
		abortIfIllFormed();
		a.doRetain(itemCount);
	}
	
	
	public function updateAttributes(u:AttributesUpdate):void {
		accu[0] = accu[0].mergeWith(a.checkUpdateAttributes(u, v));
		abortIfIllFormed();
		a.doUpdateAttributes(u);
	}
	
	
	public function annotationBoundary(map:AnnotationBoundaryMap):void {
		accu[0] = accu[0].mergeWith(a.checkAnnotationBoundary(map, v));
		abortIfIllFormed();
		a.doAnnotationBoundary(map);
	}
	
	
	public function elementEnd():void {
		accu[0] = accu[0].mergeWith(a.checkElementEnd(v));
		abortIfIllFormed();
		a.doElementEnd();
	}
	
	
	public function elementStart(type:String, attrs:Attributes):void {
		accu[0] = accu[0].mergeWith(a.checkElementStart(type, attrs, v));
		abortIfIllFormed();
		a.doElementStart(type, attrs);
	}
	
}
}